<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>获取当前日期</title>
</head>

<body>
  <script>
    function formatDate(dt) {
      // 判断dt是否为空
      //    为空赋值
      if (!dt) {
        dt = new Date();
      }

      // 获取年月日
      var year = dt.getFullYear();
      var month = dt.getMonth();
      var date = dt.getDate();

      // 格式化年月日
      if (month < 10) {
        year = '0' + year;

      }
      if (date < 10) {
        date = '0' + date;

      }

      // 组合年月日
      return year + '-' + month + '-' + date;
    }
    var nowDate = new Date();
    var a = formatDate(nowDate);
    console.log(a);
    
  </script>
</body>

</html>